<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 100px;
            height: 100px;
            position: absolute;
            left: 50px;
            top:50px;
            background: red;
        }

    </style>
</head>
<body>
    <div class="box"></div>

    <script>
        var box = document.querySelector(".box");
        box.onmousedown = function(e){
            console.log("鼠标按下");
            // 只会在鼠标点击按下的时候获取一次
            var x = e.clientX -  box.offsetLeft;
            var y  = e.clientY - box.offsetTop;
            // box.onmouseup = function(e){
            //     var xx = e.clientX - x + box.offsetLeft;
            //     var yy = e.clientY - y + box.offsetTop;
            //     console.log(xx,yy);
            //     box.style.left = xx + "px";
            //     box.style.top = yy + "px";
            // }
            box.onmousemove = function(e){
                // 在拖动的时候 box.offsetLeft 后者是offsetTop会重新获取
                // 如果加的话 offsetLeft、offsetTop 会不停的获取值 ，然后 style.Left 和style.top会不停的改变 所以offsetLeft、offsetTop的值也会不停的改变 
                // var xx = e.clientX - x ;
                // var yy = e.clientY - y  ;
                // box.style.left = xx + "px";
                // box.style.top = yy + "px";
                box.style.left = e.clientX + "px";
                box.style.top = e.clientY + "px";
            }
        }

        document.onmouseup = function(){
            box.onmousemove = "";
        }

// 给小方块 加上边界  

    </script>
</body>
</html>